package edu.xmut.kakamusic;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.KeyEvent;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.navigation.NavController;
import androidx.navigation.fragment.NavHostFragment;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;

import com.google.android.material.bottomnavigation.BottomNavigationView;

import java.util.ArrayList;
import java.util.Objects;

import edu.xmut.kakamusic.base.OnFragmentKeyDownListener;
import edu.xmut.kakamusic.utils.ToastUtils;
import edu.xmut.kakamusic.utils.WindowUtils;

public class MainActivity extends AppCompatActivity {

    private ArrayList<OnFragmentKeyDownListener> onFragmentKeyDownListeners = new ArrayList<>();
    private NavController navController;
    private long exitTime;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // 隐藏actionBar
        WindowUtils.hideActionBar(this);
        // 开启透明色状态栏
        EdgeToEdge.enable(this);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // 设置状态栏背景色
        WindowUtils.setStatusBarColor(this, Color.TRANSPARENT);
        // 设置状态栏的外观
        WindowUtils.setLightStatusBarAppearance(this, false);

        initView();
    }


    private void initView() {
        BottomNavigationView bottomNavigationView = findViewById(R.id.bottomNavigationView);
        NavHostFragment navHostFragment = (NavHostFragment) getSupportFragmentManager().findFragmentById(R.id.fragmentContainerView);
        navController = navHostFragment.getNavController();
        NavigationUI.setupWithNavController(bottomNavigationView, navController);
    }

    public void setOnFragmentKeyDownListener(OnFragmentKeyDownListener listener) {
        onFragmentKeyDownListeners.add(listener);
    }

    @Override
    public boolean onSupportNavigateUp() {
        if (!onFragmentKeyDownListeners.isEmpty()) {
            onFragmentKeyDownListeners.remove(onFragmentKeyDownListeners.size() - 1);
        }
        return NavigationUI.navigateUp(navController, new AppBarConfiguration.Builder().build());
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (Objects.equals(KeyEvent.ACTION_DOWN, event.getAction()) && Objects.equals(keyCode, KeyEvent.KEYCODE_BACK)) {
            if (!onFragmentKeyDownListeners.isEmpty()) {
                if (onFragmentKeyDownListeners.get(onFragmentKeyDownListeners.size() - 1).onKeyDown(keyCode, event)) {
                    return true;
                } else {
                    onSupportNavigateUp();
                }
            } else if (System.currentTimeMillis() - exitTime > 2000) {
                ToastUtils.show(this, "再按一次退出程序");
                exitTime = System.currentTimeMillis();
            } else {
                Intent intent = new Intent(Intent.ACTION_MAIN);
                intent.addCategory(Intent.CATEGORY_HOME);
                startActivity(intent);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}